﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Yyyg.Models;

namespace Yyyg.BLL
{
    public class GoodsService : TableServiceBase<Goods>, IBLL.IGoodsService
    {
        private int pagesize = 30;

        LuckyEntities db;
        public GoodsService(LuckyEntities db):base(db)
        {
            this.db = db;
        }

        /// <summary>
        /// 添加商品
        /// </summary>
        public bool Add(string guid, string title, decimal price, int needamo, int tyid, int stock, string brin, string main_url, string detImgsJsonUrl, string subImgsJsonUrl, bool enable = true)
        {
            Goods good = new Goods()
            {
                Title = title,
                Guid = guid,
                Price = price,
                Need_amo = needamo,
                Typeid = tyid,
                Stock = stock,
                Brin = brin,
                Mainimg_url = main_url,
                Detimgs_url = detImgsJsonUrl,
                Subimgs_url = subImgsJsonUrl,
                Enable = enable
            };
            db.Goods.Add(good);
            return db.SaveChanges() > 0;

        }

        /// <summary>
        /// 添加商品
        /// </summary>
        public bool Add(Goods good)
        {
            db.Goods.Add(good);
            return db.SaveChanges() > 0;
        }

        /// <summary>
        /// 异步查询所有商品信息
        /// </summary>
        /// <returns></returns>
        public List<Goods> GetAll()
        {
            return db.Goods.ToList();
        }

        /// <summary>
        /// 根据Guid查询商品
        /// </summary>
        /// <param name="guid"></param>
        /// <returns></returns>
        public Goods QueryGoodByGuid(string guid)
        {
            if (guid.Length == 0) return null;

            return db.Goods.FirstOrDefault(g => g.Guid == guid);

        }

        /// <summary>
        /// 根据参数异步查询商品信息
        /// </summary>
        /// <param name="qpar"></param>
        /// <returns></returns>
        public List<Goods> QueryGoods(string qpar)
        {
            if (string.IsNullOrEmpty(qpar)) return GetAll();

            return db.Goods.Where(g => g.Gid.ToString() == qpar || g.Title.Contains(qpar)).ToList();

        }

        /// <summary>
        /// 根据分页信息异步查询商品,页码从1开始
        /// </summary>
        /// <param name="pnumber"></param>
        /// <returns></returns>
        public List<Goods> QueryGoodsPage(int pnumber)
        {
            if (pagesize <= 0 || pnumber <= 0) throw new ApplicationException("页长或页码小于等于0");

            var all = GetAll();
            int sumPage = all.Count / pagesize;
            if (all.Count % pagesize != 0) sumPage++;

            return all.Skip((pnumber - 1) * pagesize).Take(pagesize).ToList();
        }

        /// <summary>
        /// 获取总页数
        /// </summary>
        /// <returns></returns>
        public int GetSumPages()
        {
            var s = db.Goods.Count();
            var sum = s / pagesize;
            if (s % pagesize > 0) sum++;
            return sum;
        }

        /// <summary>
        /// 返回商品分类一二级列表
        /// </summary>
        /// <returns></returns>
        public Pair<string, string>[][] GetTypes()
        {
            var fs = db.Goods_type.Where(t => t.Parentid < 0).Select(t => new Pair<string, string>
            {
                Value1 = t.Typename,
                Value2 = "{\"typeid\":\"" + t.Typeid + "\",\"parentid\":\"" + t.Parentid + "\"}"
            }).ToArray();
            var ss = db.Goods_type.Where(t => t.Parentid > 0).Select(t => new Pair<string, string>
            {
                Value1 = t.Typename,
                Value2 = "{\"typeid\":\"" + t.Typeid + "\",\"parentid\":\"" + t.Parentid + "\"}"
            }).ToArray();
            return new Pair<string, string>[][] { fs, ss };
        }


        public bool DelGood(string guid)
        {
            if (guid == null) return false;
            var g = db.Goods.FirstOrDefault(f => f.Guid == guid);
            if (g == null) return false;
            db.Goods.Remove(g);
            db.SaveChanges();
            return true;
        }
    }
}
